const axios = require('axios');
const fs = require('fs');
const uuid = require('node-uuid');

async function downimage(page,url) {
    console.log('跳转到网页:'+url);
    
    
   
    let imgLocalURL = []
    let imgURL = await page.evaluate(() => {
        let imgURL = []        
        let selector = '#article_content  img';
        let imgUrlList = [...document.querySelectorAll(selector)];
        imgUrlList.forEach(e => {
            imgURL.push(e.src)
        })
        return imgURL
    });
    console.log(imgURL.length);
    imgURL.forEach((e, i) => {
        
        if (e != null) {
            console.log('下载图片:'+e)
            let filename=`${uuid.v1().toString().replace('-','')}.${e.substr(e.length-3)}`
            
            imgLocalURL.push(`${filename}`)
            axios.get(e, {
                responseType: 'stream'
            }).then(res => {
               
                res.data.pipe(fs.createWriteStream(`./html/${new Date().toLocaleDateString()}/images/${filename}`));                
                
            })
        }        
    });
    return {remoteUrl:imgURL,
        localUrl:imgLocalURL
    }
}
module.exports=downimage